import {
    createRouter,
    //要不要带#号 createWebHistory,
    createWebHashHistory,
} from 'vue-router'


import { ElNotification } from 'element-plus';

import { useTokenStore } from "../store/token";


// 是否为调试模式
const debugMdoe = true;

if (debugMdoe) {
    ElNotification({
        title: '警告',
        message: `<h3>如果你看到此消息，说明当前模式为调试模式,请不要在生产环境中使用该模式。</h3> 若关闭此条消息，请到<strong style="color:red;">router/index.js</strong>将 <span style="color:red;">debugMode</span>改为 <span style="color:blue;">false</span>即可`,
        type: 'warning',
        dangerouslyUseHTMLString: true,
        // showClose:false,
        duration: (2 * 1000)
    })

}

// 调试路由页面
let _routes = [
    {
        path: '/My',
        name: 'My',
        component: () => import('../pages/My.vue'),
    },{
        path: '/login',
        name: 'login',
        component: () =>
            import ("../pages/Login.vue")
    }, {
        path: '/admin',
        name: 'home',
        children: [{
                path: '',
                // component: () => import("../pages/admin/Home.vue")
                component: () =>
                    import ("../components/FriendMap.vue")
            },
            {
                path: 'friend',
                component: () =>
                    import ("../pages/admin/friend.vue")
            },
            // {
            //     path: 'state-info',
            //     component: () =>
            //         import ("../pages/NotFound.vue")
            // },
            {
                path: 'state-node',
                component: () =>
                    import ("../components/NodeState.vue")
            },
            {
                path: 'about',
                name: 'about',
                component: () =>
                    import ("../pages/admin/About.vue")
            },
            {
                path: 'message',
                name: 'message',
                component: () =>
                    import ("../pages/admin/Message.vue")
            }
        ],
        component: () =>
            import ("../pages/Admin.vue")
    },
    {
        path: '/register',
        name: 'Register',
        component: () =>
            import ("../pages/Register.vue")
    },
    // 没匹配的
    // {
    //   path: '*',
    //   name: 'NotFound',
    //   component: ()=>import("../pages/NotFound.vue")
    // },
    // 开发页
    {
        path: '/helloWorld',
        name: 'HelloWorld',
        component: () =>
            import ("../pages/HelloWorld.vue")
    },
    //  不能作为 页面  因为他是一个组件 内部嵌套使用
    // {
    //   path: '/friendMap',
    //   name: 'FriendMap',
    //   component: ()=>import("../pages/FriendMap.vue")
    // }
    // {
    //     path: '/my',
    //     name: 'My',
    //     component: () =>
    //         import ("../pages/message.vue"),
    //     children: [ {
    //             path: "/",
    //             name: 'Primary',
    //             component: () =>
    //                 import ("../pages/my/Primary.Vue"),
    //         },
    //         {
    //             path: "/",
    //             name: 'High',
    //             component: () =>
    //                 import ("../pages/my/High.Vue"),
    //         }
    //     ]
    // }
]


const router = createRouter({
    history: createWebHashHistory(),
    // history: createWebHistory(),
    routes: (!debugMdoe) ? routes : _routes
})

router.beforeEach((to, from, next) => {
    // 放行登录
    if (to.path == "/login") {
        next();
    } else {
        const tokenStore = useTokenStore();
        // 权限认证
        if (tokenStore.token != '') {
            next()
        } else {
            ElNotification.warning("需要登录");
            next("/login")
        }
    }
})

export default router;
